﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXShop.IDAL.Store;
using System.Data;
using System.Data.SqlClient;

namespace YXShop.SQLServerDAL.Store
{
    public class StoreAsk : IStoreAsk
    {
        #region DataBase Operation

        public int Add(YXShop.Model.Store.StoreAsk model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into yxs_storeask(");
            strSql.Append("senderid,sendertime,messagetitle,message,receivetype,receivestoreid,receivestorecenterid,answercount)");
            strSql.Append(" values(");
            strSql.Append("@senderid,@sendertime,@messagetitle,@message,@receivetype,@receivestoreid,@receivestorecenterid,@answercount)");
            strSql.Append(" Select scope_IDENTITY()");
            SqlParameter[] parameters = (SqlParameter[])this.ValueParas(model);
            object obj = ChangeHope.DataBase.SQLServerHelper.GetSingle(strSql.ToString(),parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public int Update(YXShop.Model.Store.StoreAsk model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [yxs_storeask] set ");
            strSql.Append("senderid=@senderid,sendertime=@sendertime,messagetitle=@messagetitle,message=@message,receivetype=@receivetype,receivestoreid=@receivestoreid,receivestorecenterid=@receivestorecenterid,answercount=@answercount");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameters = (SqlParameter[])this.ValueParas(model);
            return ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(), parameters);
        }

        public void Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete  from [yxs_storeask] ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameter = { new SqlParameter("@id",SqlDbType.Int,4)};
            parameter[0].Value = id;
            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(),parameter);

        }


        public void DeleteAll(string ids)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [yxs_storeask] where id in(");
            strSql.Append(" "+ids+" )");
            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString());
        }
      
        #endregion

        #region DataBase Load

        public YXShop.Model.Store.StoreAsk GetModelById(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from yxs_storeask ");
            strSql.Append(" where id=@id ");
            SqlParameter[] parameter ={
                                          new SqlParameter("@id",SqlDbType.Int,4)
                                     };
            parameter[0].Value = id;
            YXShop.Model.Store.StoreAsk model = null;
            using(SqlDataReader reader=ChangeHope.DataBase.SQLServerHelper.ExecuteReader(strSql.ToString(),parameter))
            {
                while(reader.Read())
                {
                    model = new YXShop.Model.Store.StoreAsk();
                    model.Id = Convert.ToInt32(reader["id"]);
                    model.SenderId = Convert.ToInt32(reader["senderid"]);
                    model.SenderTime = Convert.ToDateTime(reader["sendertime"]);
                    model.MessageTitle = Convert.ToString(reader["messagetitle"]);
                    model.Message = Convert.ToString(reader["message"]);
                    model.ReceiveType = Convert.ToInt32(reader["receivetype"]);
                    model.ReceiveStoreId = Convert.ToInt32(reader["receivestoreid"]);
                    model.ReceiveStoreCenterId = Convert.ToInt32(reader["receivestorecenterid"]);
                    model.Answercount = Convert.ToInt32(reader["answercount"]);
                }
            }
            return model;
        }

        public List<YXShop.Model.Store.StoreAsk> GetListStoreAskByWhere(string where)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from yxs_storeask ");
            if(where!=null&&where!="")
            {
                strSql.Append(" where 1=1 "+where+" ");
            }
            List<YXShop.Model.Store.StoreAsk> storeList = new List<YXShop.Model.Store.StoreAsk>();
            using (SqlDataReader reader = ChangeHope.DataBase.SQLServerHelper.ExecuteReader(strSql.ToString()))
            {
                while (reader.Read())
                {
                    YXShop.Model.Store.StoreAsk model = new YXShop.Model.Store.StoreAsk();
                    model.Id = Convert.ToInt32(reader["id"]);
                    model.SenderId = Convert.ToInt32(reader["senderid"]);
                    model.SenderTime = Convert.ToDateTime(reader["sendertime"]);
                    model.MessageTitle = Convert.ToString(reader["messagetitle"]);
                    model.Message = Convert.ToString(reader["message"]);
                    model.ReceiveType = Convert.ToInt32(reader["receivetype"]);
                    model.ReceiveStoreId = Convert.ToInt32(reader["receivestoreid"]);
                    model.ReceiveStoreCenterId = Convert.ToInt32(reader["receivestorecenterid"]);
                    model.Answercount = Convert.ToInt32(reader["answercount"]);
                    storeList.Add(model);
                }
            }
            return storeList;
        }
        public ChangeHope.DataBase.DataByPage GetList(string strWhere)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("[select] * [from] yxs_storeask [where] 1=1 ");
            if(strWhere!=null&&strWhere!="")
            {
                strSql.Append(""+strWhere);
            }
            strSql.Append(" [order by] id desc");
            dataPage.Sql = strSql.ToString();
            dataPage.GetRecordSetByPage();
            return dataPage;
        }

        /// <summary>
        /// 排序所有记录
        /// </summary>
        /// <remarks></remarks>
        public ChangeHope.DataBase.DataByPage GetList(string orderfield, int pagesize, string Conditions)
        {
            ChangeHope.DataBase.DataByPage dataPage = new ChangeHope.DataBase.DataByPage();
            dataPage.Sql = "[select] * [from] yxs_storeask [where] 1=1 " + Conditions + " " + orderfield;
            dataPage.PageSize = pagesize;
            dataPage.GetRecordSetByPage();
            return dataPage;
        }

        #endregion

        #region Other Function
        public IDbDataParameter[] ValueParas(YXShop.Model.Store.StoreAsk model)
        {

            SqlParameter[] parameters ={
                                          new SqlParameter("@id",SqlDbType.Int,4),
                                          new SqlParameter("@senderid",SqlDbType.Int,4),
                                          new SqlParameter("@sendertime",SqlDbType.DateTime),
                                          new SqlParameter("@messagetitle",SqlDbType.NVarChar,255),
                                          new SqlParameter("message",SqlDbType.NText),
                                          new SqlParameter("@receivetype",SqlDbType.Int,4),
                                          new SqlParameter("@receivestoreid",SqlDbType.Int,4),
                                          new SqlParameter("@receivestorecenterid",SqlDbType.Int,4),
                                          new SqlParameter("@answercount",SqlDbType.Int,4),

                                      };
            parameters[0].Value = model.Id;
            parameters[1].Value = model.SenderId;
            parameters[2].Value = model.SenderTime;
            parameters[3].Value = model.MessageTitle;
            parameters[4].Value = model.Message;
            parameters[5].Value = model.ReceiveType;
            parameters[6].Value = model.ReceiveStoreId;
            parameters[7].Value = model.ReceiveStoreCenterId;
            parameters[8].Value = model.Answercount;

            return parameters;
        }
        #endregion

       
    }
}
